package VISTA.ventanas;

import BD.Pagoempleados;
import BD.Pagoscliente;
import BD.Pagosproveedores;
import BD.TableModel;
import NEGOCIOS.ControlPagoClientes;
import NEGOCIOS.ControlUniversalBD;
import NEGOCIOS.ControlPagoEmpleados;
import NEGOCIOS.ControlPagoProveedores;
import Utilidades.ManejadorFechas;
import java.beans.PropertyVetoException;
import java.util.List;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import reportes.Modulos.ModuloReporte;

public class CatalogoDePagos extends javax.swing.JInternalFrame {

    private String pestañaActiva = "";

    public CatalogoDePagos() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        jtpPagos = new javax.swing.JTabbedPane();
        jpClientes = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtClientes = new javax.swing.JTable();
        jpProveedores = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtproveedores = new javax.swing.JTable();
        jpEmpleados = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jtEmpleados = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jbNuevo = new javax.swing.JButton();
        jbQuitar = new javax.swing.JButton();
        jbsalir = new javax.swing.JButton();
        jbReporte = new javax.swing.JButton();
        jbBuscar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(204, 0, 0));
        setClosable(true);
        setResizable(true);
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/Catalog.png"))); // NOI18N
        setPreferredSize(new java.awt.Dimension(888, 500));
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                formKeyTyped(evt);
            }
        });

        jPanel5.setBackground(new java.awt.Color(153, 51, 0));

        jtpPagos.setBackground(new java.awt.Color(204, 0, 0));

        jpClientes.setBackground(new java.awt.Color(255, 255, 255));
        jpClientes.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jpClientesComponentShown(evt);
            }
        });
        jpClientes.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jpClientesFocusGained(evt);
            }
        });

        jtClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Clave", "Cliente", "Fecha", "Total"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtClientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtClientesMouseClicked(evt);
            }
        });
        jtClientes.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                jtClientesInputMethodTextChanged(evt);
            }
        });
        jtClientes.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jtClientesPropertyChange(evt);
            }
        });
        jtClientes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtClientesKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtClientesKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtClientesKeyTyped(evt);
            }
        });
        jScrollPane2.setViewportView(jtClientes);

        javax.swing.GroupLayout jpClientesLayout = new javax.swing.GroupLayout(jpClientes);
        jpClientes.setLayout(jpClientesLayout);
        jpClientesLayout.setHorizontalGroup(
            jpClientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpClientesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
                .addContainerGap())
        );
        jpClientesLayout.setVerticalGroup(
            jpClientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpClientesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                .addContainerGap())
        );

        jtpPagos.addTab("Clientes", jpClientes);

        jpProveedores.setBackground(new java.awt.Color(255, 255, 255));
        jpProveedores.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jpProveedoresComponentShown(evt);
            }
        });

        jtproveedores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Clave", "Proveedor", "Fecha", "RFC", "Total"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtproveedores.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtproveedoresMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jtproveedores);
        jtproveedores.getColumnModel().getColumn(0).setPreferredWidth(50);
        jtproveedores.getColumnModel().getColumn(0).setMaxWidth(50);

        javax.swing.GroupLayout jpProveedoresLayout = new javax.swing.GroupLayout(jpProveedores);
        jpProveedores.setLayout(jpProveedoresLayout);
        jpProveedoresLayout.setHorizontalGroup(
            jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 690, Short.MAX_VALUE)
            .addGroup(jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpProveedoresLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jpProveedoresLayout.setVerticalGroup(
            jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 376, Short.MAX_VALUE)
            .addGroup(jpProveedoresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpProveedoresLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        jtpPagos.addTab("Proveedores", jpProveedores);

        jpEmpleados.setBackground(new java.awt.Color(255, 255, 255));
        jpEmpleados.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jpEmpleadosComponentShown(evt);
            }
        });

        jtEmpleados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Clave", "Empleado", "Fecha", "Modelo", "Cantidad", "Total"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtEmpleados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtEmpleadosMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(jtEmpleados);

        javax.swing.GroupLayout jpEmpleadosLayout = new javax.swing.GroupLayout(jpEmpleados);
        jpEmpleados.setLayout(jpEmpleadosLayout);
        jpEmpleadosLayout.setHorizontalGroup(
            jpEmpleadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 690, Short.MAX_VALUE)
            .addGroup(jpEmpleadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpEmpleadosLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jpEmpleadosLayout.setVerticalGroup(
            jpEmpleadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 376, Short.MAX_VALUE)
            .addGroup(jpEmpleadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpEmpleadosLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        jtpPagos.addTab("Empleados", jpEmpleados);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jbNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/New.png"))); // NOI18N
        jbNuevo.setText("Nuevo pago");
        jbNuevo.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbNuevoActionPerformed(evt);
            }
        });

        jbQuitar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/delete.png"))); // NOI18N
        jbQuitar.setText("Eliminar pago");
        jbQuitar.setEnabled(false);
        jbQuitar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbQuitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbQuitarActionPerformed(evt);
            }
        });

        jbsalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/Cancel.png"))); // NOI18N
        jbsalir.setText("Salir");
        jbsalir.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbsalirActionPerformed(evt);
            }
        });

        jbReporte.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/Report.png"))); // NOI18N
        jbReporte.setText("Generar reporte");
        jbReporte.setEnabled(false);
        jbReporte.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbReporteActionPerformed(evt);
            }
        });

        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/Search.png"))); // NOI18N
        jbBuscar.setText("Buscar");
        jbBuscar.setEnabled(false);
        jbBuscar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jbQuitar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbReporte, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(jbNuevo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbsalir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jbBuscar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbQuitar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbReporte, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbsalir, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jbNuevo, jbQuitar, jbReporte, jbsalir});

        jLabel1.setBackground(new java.awt.Color(255, 204, 0));
        jLabel1.setFont(new java.awt.Font("Brush Script MT", 3, 36)); // NOI18N
        jLabel1.setText(" Catálogo de pagos");
        jLabel1.setOpaque(true);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jtpPagos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtpPagos, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jpClientesFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jpClientesFocusGained
    }//GEN-LAST:event_jpClientesFocusGained

    //Llena la tabla de pagos para clientes
    private void jpClientesComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jpClientesComponentShown
        //Solo hay que dar el nombre de la tabla
        ControlPagoClientes cpe = new ControlPagoClientes();
        if (cpe.seleccionarTodosPagosClientes().size() > 0) {
            cargaTabla(jtClientes);
            habilitaComponentes();
        } else {
            deshabilitaComponentes();
        }
    }//GEN-LAST:event_jpClientesComponentShown

    private void jbBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscarActionPerformed
        //se presiono el boton
        String msg = JOptionPane.showInputDialog("ingrese el nombre "
                + "que desa buscar");
        int filas = jtClientes.getRowCount();
        int columnas = jtClientes.getColumnCount();
        String val = msg;

        //Realiza la busqueda de un elemento en la tabla
        for (int fila = 0; fila < filas; fila++) {
            for (int columna = 0; columna < columnas; columna++) {
                String word = String.valueOf(jtClientes.getValueAt(fila,
                        columna));
                if (val == null ? word == null
                        : val.toLowerCase().equals(word.toLowerCase())) {
                    jtClientes.changeSelection(fila, columna, false, false);
                    JOptionPane.showMessageDialog(jtClientes,
                            "Ha sido localizado la información: "
                            + msg + "\nfila: "
                            + (fila + 1) + "\ncolumna: "
                            + (columna + 1));
                    columna = columnas;
                    fila = filas;
                }
            }
        }        // TODO add your handling code here:
    }//GEN-LAST:event_jbBuscarActionPerformed

    private void jbQuitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbQuitarActionPerformed
        GuardarTrueEliminarFalse(false);
    }//GEN-LAST:event_jbQuitarActionPerformed

    //Si guardar es falso entonces la accion es de eliminar
    private void GuardarTrueEliminarFalse(boolean guardar) {
        //Revisa si hay una fila seleccionada
        int fila = 0;
        ControlUniversalBD CUBD = new ControlUniversalBD();
        switch (pestañaActiva) {
            case "Clientes":
                fila = jtClientes.getSelectedRow();
                Pagoscliente pagosCliente = new Pagoscliente();
                pagosCliente.setId(Integer.parseInt(
                        jtClientes.getValueAt(fila, 0).toString()));
                pagosCliente.setFecha(ManejadorFechas.deStringToDate(
                        jtClientes.getValueAt(fila, 1).toString()));
                pagosCliente.setNombre(
                        jtClientes.getValueAt(fila, 2).toString());
                pagosCliente.setTotal(Double.parseDouble(
                        jtClientes.getValueAt(fila, 3).toString()));
                if (guardar) {
                    CUBD.Update(pagosCliente);
                } else {
                    CUBD.delete(pagosCliente);
                }
                cargaTabla(jtClientes);
                break;
            case "Empleados":
                fila = jtEmpleados.getSelectedRow();
                Pagoempleados pagoEmpleados = new Pagoempleados();
                pagoEmpleados.setId(Integer.parseInt(
                        jtEmpleados.getValueAt(fila, 0).toString()));
                pagoEmpleados.setFecha(ManejadorFechas.deStringToDate(
                        jtEmpleados.getValueAt(fila, 1).toString()));
                pagoEmpleados.setNombre(
                        jtEmpleados.getValueAt(fila, 2).toString());
                pagoEmpleados.setTipopago(
                        jtEmpleados.getValueAt(fila, 3).toString().charAt(0));
                pagoEmpleados.setModelo(
                        jtEmpleados.getValueAt(fila, 4).toString());
                pagoEmpleados.setCantidad(
                        Integer.parseInt(
                                jtEmpleados.getValueAt(fila, 5).toString()));
                pagoEmpleados.setPrecio(
                        Double.parseDouble(
                                jtEmpleados.getValueAt(fila, 6).toString()));
                pagoEmpleados.setTotal(
                        Double.parseDouble(
                                jtEmpleados.getValueAt(fila, 7).toString()));
                if (guardar) {
                    CUBD.Update(pagoEmpleados);
                } else {
                    CUBD.delete(pagoEmpleados);
                }
                cargaTabla(jtEmpleados);
                break;
            case "Proveedores":
                fila = jtproveedores.getSelectedRow();
                Pagosproveedores pr = new Pagosproveedores();
                pr.setId(Integer.parseInt(
                        jtproveedores.getValueAt(fila, 0).toString()));
                pr.setFecha(ManejadorFechas.deStringToDate(
                        jtproveedores.getValueAt(fila, 1).toString()));
                pr.setNombre(jtproveedores.getValueAt(fila, 2).toString());
                pr.setRfc(jtproveedores.getValueAt(fila, 3).toString());
                pr.setTotal(Double.parseDouble(
                        jtproveedores.getValueAt(fila, 4).toString()));
                if (guardar) {
                    CUBD.Update(pr);
                } else {
                    CUBD.delete(pr);
                }
                cargaTabla(jtproveedores);
                break;
        }
    }

    private void jbsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbsalirActionPerformed
        try {
            this.setClosed(true);
            System.gc();
        } catch (PropertyVetoException ex) {
            this.dispose();
            System.gc();
        }
    }//GEN-LAST:event_jbsalirActionPerformed

    private void jpProveedoresComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jpProveedoresComponentShown
        //Solo hay que dar el nombre de la tabla
        ControlPagoProveedores cpp = new ControlPagoProveedores();
        if (cpp.seleccionarTodosLosPagos().size() > 0) {
            cargaTabla(jtproveedores);
            habilitaComponentes();
        } else {
            deshabilitaComponentes();
        }
    }//GEN-LAST:event_jpProveedoresComponentShown
    private void habilitaComponentes() {
        jbBuscar.setEnabled(true);
        jbQuitar.setEnabled(true);
        jbReporte.setEnabled(true);
    }

    private void deshabilitaComponentes() {
        jbBuscar.setEnabled(false);
        jbQuitar.setEnabled(false);
        jbReporte.setEnabled(false);
    }
    private void jpEmpleadosComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jpEmpleadosComponentShown
        ControlPagoEmpleados cpe = new ControlPagoEmpleados();
        if (cpe.seleccionarTodosLosPagos().size() > 0) {
            cargaTabla(jtEmpleados);
            habilitaComponentes();
        } else {
            deshabilitaComponentes();
        }
    }//GEN-LAST:event_jpEmpleadosComponentShown

    //Llama a una nueva ventana interna
    private void jbNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbNuevoActionPerformed
        switch (pestañaActiva) {
            case "Clientes":
                //Instancia la ventana
                PagosDeClientes pc = new PagosDeClientes();
                //La manda a invocar con este metodo
                invocadorDeVentanas(pc);
                break;
            case "Empleados":
                PagosDeEmpleados pe = new PagosDeEmpleados();
                invocadorDeVentanas(pe);
                break;
            case "Proveedores":
                PagosDeProveedores pp = new PagosDeProveedores();
                invocadorDeVentanas(pp);
                break;
        }// TODO add your handling code here:
    }//GEN-LAST:event_jbNuevoActionPerformed

    private void editaTablaEnTiempoReal(JTable tabla) {
        if (tabla.getSelectedColumn() > 1) {
            String msg = "";
            msg = JOptionPane.showInputDialog("Ingrese el nuevo dato:");
            if (!msg.trim().equals("")) {
                TableModel model = (TableModel) tabla.getModel();
                model.setValueAt(msg, tabla.getSelectedRow(),
                        tabla.getSelectedColumn());
                GuardarTrueEliminarFalse(true);
            }
        }
    }

    private void jtClientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtClientesMouseClicked
        editaTablaEnTiempoReal(jtClientes);
    }//GEN-LAST:event_jtClientesMouseClicked

    private void jtClientesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtClientesKeyPressed

    }//GEN-LAST:event_jtClientesKeyPressed

    private void jtClientesKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtClientesKeyTyped

    }//GEN-LAST:event_jtClientesKeyTyped

    private void jtClientesInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jtClientesInputMethodTextChanged

    }//GEN-LAST:event_jtClientesInputMethodTextChanged

    private void jtClientesKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtClientesKeyReleased

    }//GEN-LAST:event_jtClientesKeyReleased

    private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_formKeyTyped

    private void jtClientesPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jtClientesPropertyChange

    }//GEN-LAST:event_jtClientesPropertyChange

    private void jtproveedoresMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtproveedoresMouseClicked
        editaTablaEnTiempoReal(jtproveedores);
    }//GEN-LAST:event_jtproveedoresMouseClicked

    private void jtEmpleadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtEmpleadosMouseClicked
        editaTablaEnTiempoReal(jtEmpleados);
    }//GEN-LAST:event_jtEmpleadosMouseClicked

    private void jbReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbReporteActionPerformed
        //Invoco el reporte segun la pestaña activa
        try {
            ModuloReporte invocaReporte = new ModuloReporte(pestañaActiva);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Ha ocurrido un error: " + e, "Error", 0);
        }
    }//GEN-LAST:event_jbReporteActionPerformed

    //Comprueba la existencia en pantalla de una ventana interna
    private boolean existeEnPantalla(JInternalFrame ventana) throws Exception {
        try {
            //obtiene un arreglo de ventanas abiertas
            JInternalFrame[] ventanasAbiertas
                    = mdiPrincipal.desktop.getAllFrames();
            for (int i = 0; i < ventanasAbiertas.length; i++) {
                if (ventanasAbiertas[i].getTitle().equals(ventana.getTitle())) {
                    ventanasAbiertas[i].moveToFront();
                    ventanasAbiertas[i].setSelected(true);
                    ventanasAbiertas[i].setLocation(
                            (mdiPrincipal.desktop.getWidth()
                            - ventanasAbiertas[i].getWidth()) / 2,
                            (mdiPrincipal.desktop.getHeight()
                            - ventanasAbiertas[i].getHeight()) / 2);
                    return true;
                }
            }
        } catch (Exception e) {
            throw e;
        }
        return false;
    }

    private void invocadorDeVentanas(JInternalFrame ventana) {
        try {
            //Verifica la existencia en pantalla de la ventana interna
            //Con este método
            if (existeEnPantalla(ventana) == false) {
                mdiPrincipal.desktop.add(ventana);
                ventana.setVisible(true);
                ventana.setLocation(
                        (mdiPrincipal.desktop.getWidth()
                        - ventana.getWidth()) / 2,
                        (mdiPrincipal.desktop.getHeight()
                        - ventana.getHeight()) / 2);
            } else {
                ventana.dispose();
            }
            ventana.setVisible(true);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,
                    "Ha ocurrido un error: " + e, "Error", 0);
        }
    }

    private void cargaTabla(final JTable jtTabla) {
        //--------------Refrescando la tabla----------------------
        jtTabla.setVisible(false);
        //Creo la lista que guardara los registros
        List listRegistros = null;
        //Guardo los registros obtenidos en un lista
        listRegistros = obtenRegistros();
        //Compruebo que la lista alla traido registros
        if (listRegistros.size() > 0) {
            habilitaBotones(true);
            //Limpio la tabla
            jtTabla.removeAll();
            //Creo el modelo para cargarlo en la tabla
            TableModel tableModel
                    = new TableModel(pestañaActiva, listRegistros);
            //Cargo la tabla
            jtTabla.setModel(tableModel);
            jtTabla.setVisible(true);
        } else {
            habilitaBotones(false);
            jtTabla.removeAll();
        }
        //--------------Refrescando la tabla----------------------

    }

    private void habilitaBotones(boolean activar) {
        //Si la tabla tiene filas se habilitan los botones si no lo contrario
        jbReporte.setEnabled(activar);
        jbBuscar.setEnabled(activar);
        jbQuitar.setEnabled(activar);
    }

    //Retorna una lista llena de registros
    private List obtenRegistros() {
        //Esta lista guardará los objetos de la consulta
        List listEntity = null;
        //Realiza la consulta para la pestaña activa
        if (jpClientes.isShowing()) {
            //ControlPagoClientes cpClientes = new ControlPagoClientes();
            ControlUniversalBD cpUniversal = new ControlUniversalBD();
            //Recibo la consulta en la lista
            //listEntity = cpClientes.seleccionarTodosPagosClientes();
            listEntity = cpUniversal.select("from Pagoscliente");
            //Defino la pestaña activa
            pestañaActiva = "Clientes";
        }
        //Realiza la consulta para la pestaña activa
        if (jpEmpleados.isShowing()) {
            ControlPagoEmpleados cpEmpleados = new ControlPagoEmpleados();
            //Recibo la consulta en la lista
            listEntity = cpEmpleados.seleccionarTodosLosPagos();
            //Defino la pestaña activa
            pestañaActiva = "Empleados";
        }
        //Realiza la consulta para la pestaña activa
        if (jpProveedores.isShowing()) {
            //Recibo la consulta en la lista
            ControlUniversalBD cpUniversal = new ControlUniversalBD();
            listEntity = cpUniversal.select("from Pagosproveedores");
            //Defino la pestaña activa
            pestañaActiva = "Proveedores";
        }
//        Retorna la lista con los objetos de la consulta
        return listEntity;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbNuevo;
    private javax.swing.JButton jbQuitar;
    private javax.swing.JButton jbReporte;
    private javax.swing.JButton jbsalir;
    private javax.swing.JPanel jpClientes;
    private javax.swing.JPanel jpEmpleados;
    private javax.swing.JPanel jpProveedores;
    private javax.swing.JTable jtClientes;
    private javax.swing.JTable jtEmpleados;
    private javax.swing.JTabbedPane jtpPagos;
    private javax.swing.JTable jtproveedores;
    // End of variables declaration//GEN-END:variables
}
